package cate.game.framework;

import cate.game.GameBody;
import cate.game.event.EventPublisher;
import cate.game.log.GameLoggerFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.slf4j.Logger;
import org.springframework.data.annotation.Transient;

public abstract class ModuleMan implements ModuleManager, ISpanable, EventPublisher {
    @Transient
    @JsonIgnore
    protected Logger log;
    @Transient
    @JsonIgnore
    protected GameBody game;

    @Override
    public void initIndependent(GameBody game) throws Exception {
        this.game = game;
        log = GameLoggerFactory.getLogger(game, this.getClass());
    }

    /**
     * 实现
     */
    @Override
    public void onDaySpan(){

    }

    /**
     * 调用
     */
    public void onDaySpanSafe(){
        try{
            onDaySpan();
        }catch (Exception e){
            if(log != null) {
                log.error("{}跨天异常", getClass().getName(), e);
            } else {
                e.printStackTrace();
            }
        }
    }

    /**
     * 实现
     */
    @Override
    public void onWeekSpan(){

    }

    /**
     * 调用
     */
    public void onWeekSpanSafe(){
        try{
            onWeekSpan();
        }catch (Exception e){
            if(log != null) {
                log.error("{}跨周异常", getClass().getName(), e);
            } else {
                e.printStackTrace();
            }
        }
    }

    /**
     * 实现
     */
    @Override
    public void onMonthSpan(){

    }

    /**
     * 调用
     */
    public void onMonthSpanSafe(){
        try{
            onMonthSpan();
        }catch (Exception e){
            if(log != null) {
                log.error("{}跨月异常", getClass().getName(), e);
            } else {
                e.printStackTrace();
            }
        }
    }

    /**
     * 调用
     */
    public void shutdownSafe(){
        try{
            shutdown();
        }catch (Exception e){
            if(log != null) {
                log.error("{}shutdown异常", getClass().getName(), e);
            } else {
                e.printStackTrace();
            }
        }
    }
}
